package adsi.utils;

import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateTime {

    public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";

    public static String now() {
        Calendar cal = GregorianCalendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);

        return sdf.format(cal.getTime());
    }

    public static String today() {
        Calendar cal = GregorianCalendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        return sdf.format(cal.getTime());
    }

    public static Calendar getToday() {
        Calendar c = Calendar.getInstance();
        return new GregorianCalendar(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
    }

    public static String toSql() {
        Calendar cal = new GregorianCalendar();
        return toSql(cal.getTime());
    }

    public static String toSql(Calendar cal) {
        if (cal != null) {
            return toSql(cal.getTime());
        } else {
            return null;
        }
    }

    public static String toSql(Date dateTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return "'" + sdf.format(dateTime) + "'";
    }


    public static Calendar DateToCalendar(Date date) {
        Calendar c = null;

        if (date != null) {
            c = new GregorianCalendar();
            c.setTime(date);
        }

        return c;
    }

    public static Calendar stringToCal(String date) {
        Calendar c = null;

        try {
            String[] aux = date.split("/");
            int year = Integer.parseInt(aux[2]);
            int month = Integer.parseInt(aux[1]) - 1; //VITAL

            int day = Integer.parseInt(aux[0]);
            c = new GregorianCalendar(year, month, day);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return c;
    }

    public static String calToString(Calendar c) {
        String result = "";

        if (c != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            result = sdf.format(c.getTime());
        }

        return result;
    }
}